﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Pixysoft.Framework.Noebe
{
    class SliceAdvNoebeCommand : INoebeCommand
    {
        List<INoebeCommand> commands = null;

        NoebeParameterCollection collection = new NoebeParameterCollection();

        string sql = null;

        public SliceAdvNoebeCommand(List<INoebeCommand> commands)
        {
            if (commands == null || commands.Count == 0)
                throw new Exception("missing commands for command.adapter");

            this.commands = commands;
        }



        public int Insert(System.Data.DataTable table)
        {
            int counter = 0;

            foreach (INoebeCommand command in commands)
            {
                counter += command.Insert(table);
            }

            return counter;
        }

        public int Insert(System.Data.DataRow row)
        {
            int counter = 0;

            foreach (INoebeCommand command in commands)
            {
                counter += command.Insert(row);
            }

            return counter;
        }

        public int AutoInsert(System.Data.DataTable table)
        {
            int counter = 0;

            foreach (INoebeCommand command in commands)
            {
                counter += command.AutoInsert(table);
            }

            return counter;
        }

        public int AutoInsert(System.Data.DataRow row)
        {
            int counter = 0;

            foreach (INoebeCommand command in commands)
            {
                counter += command.AutoInsert(row);
            }

            return counter;
        }

        public int Update(System.Data.DataTable table)
        {
            int counter = 0;

            foreach (INoebeCommand command in commands)
            {
                counter += command.Update(table);
            }

            return counter;
        }

        public int Update(System.Data.DataRow row)
        {
            int counter = 0;

            foreach (INoebeCommand command in commands)
            {
                counter += command.Update(row);
            }

            return counter;
        }

        public int Delete(System.Data.DataTable table)
        {
            int counter = 0;

            foreach (INoebeCommand command in commands)
            {
                counter += command.Delete(table);
            }

            return counter;
        }

        public int Delete(System.Data.DataRow row)
        {
            int counter = 0;

            foreach (INoebeCommand command in commands)
            {
                counter += command.Delete(row);
            }

            return counter;
        }



        public bool IsOpen
        {
            get
            {
                foreach (INoebeCommand command in commands)
                    if (!command.IsOpen)
                        return false;
                return true;
            }
        }

        public void Begin()
        {
            foreach (INoebeCommand command in commands)
                command.Begin();
        }

        public void Begin(IsolationLevel level)
        {
            foreach (INoebeCommand command in commands)
                command.Begin(level);
        }
        /// <summary>
        /// 使用安全区域操作事务
        /// </summary>
        public void Begin(NoebeTransactionArea transactionArea)
        {
            Begin(IsolationLevel.ReadCommitted, transactionArea);
        }
        /// <summary>
        /// 使用事务安全区操作
        /// </summary>
        /// <param name="level"></param>
        /// <param name="area"></param>
        public void Begin(IsolationLevel level, NoebeTransactionArea transactionArea)
        {
            Begin(level);

            try
            {
                transactionArea();

                if (this.IsOpen)
                {
                    Commit();
                }
            }
            catch (Exception ex)
            {
                Rollback();

                throw ex;
            }
        }

        public void Commit()
        {
            foreach (INoebeCommand command in commands)
                command.Commit();
        }

        public void Rollback()
        {
            foreach (INoebeCommand command in commands)
                command.Rollback();
        }

        public NoebeParameterCollection Parameters
        {
            get { return collection; }
        }

        public System.Data.DataTable ExecuteReader()
        {
            DataTable table = null;

            foreach (INoebeCommand command in commands)
            {
                command.SQL = this.sql;

                command.Parameters.ReLoad(this.collection.TypeList, this.collection.ValueList);

                DataTable rtable = command.ExecuteReader();

                if (rtable == null)
                    continue;

                if (table == null)
                {
                    table = rtable;

                    continue;
                }

                Pixysoft.Data.TableMapping.Instance.IncMappingManyToMany(rtable, table);
            }

            return table;


        }

        public string SQL
        {
            get
            {
                return sql;
            }
            set
            {
                sql = value;
            }
        }

        public int Execute()
        {
            int counter = 0;

            foreach (INoebeCommand command in commands)
            {
                command.SQL = this.sql;

                command.Parameters.ReLoad(collection.TypeList, collection.ValueList);

                counter += command.Execute();
            }

            return counter;
        }



        public DataTable Select(string tableName)
        {
            DataTable table = null;

            foreach (INoebeCommand command in commands)
            {
                DataTable rtable = command.Select(tableName);

                if (rtable == null)
                    continue;

                if (table == null)
                {
                    table = rtable;

                    continue;
                }

                Pixysoft.Data.TableMapping.Instance.IncMappingManyToMany(rtable, table);
            }

            return table;
        }

        public DataTable SelectByPk(string tableName, params object[] values)
        {
            DataTable table = null;

            foreach (INoebeCommand command in commands)
            {
                DataTable rtable = command.SelectByPk(tableName, values);

                if (rtable == null)
                    continue;

                if (table == null)
                {
                    table = rtable;

                    continue;
                }

                Pixysoft.Data.TableMapping.Instance.IncMappingManyToMany(rtable, table);
            }

            return table;
        }



        public void CascadeDelete(System.Data.DataTable table)
        {
            foreach (INoebeCommand command in commands)
            {
                command.CascadeDelete(table);
            }
        }

        public void CascadeDelete(System.Data.DataRow row)
        {
            foreach (INoebeCommand command in commands)
            {
                command.CascadeDelete(row);
            }
        }


        public void Dispose()
        {
            foreach (INoebeCommand command in commands)
            {
                command.Dispose();
            }
        }

    }
}
